package com.huawei.esdk.tp.northcommu.cxf.common;

import org.apache.log4j.Logger;

import com.huawei.esdk.platform.common.SDKErrorCode;
import com.huawei.esdk.platform.common.exception.SDKException;
import com.huawei.esdk.tp.business.common.sitectrl.SiteCtrlService;
import com.huawei.esdk.tp.common.ErrInfo;
import com.huawei.esdk.tp.domain.model.bean.CameraControl;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.ConnectSites;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.ConnectSitesResponse;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.CtrlAuxStream;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.CtrlAuxStreamResponse;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.CtrlCamera;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.CtrlCameraResponse;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.DisconnectSites;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.DisconnectSitesResponse;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.MicMuteSites;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.MicMuteSitesResponse;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.SpkQuietSites;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.SpkQuietSitesResponse;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.TPCommonSiteCtr;

public class TPCommonSiteCtrImpl implements TPCommonSiteCtr
{
    private static final Logger LOGGER = Logger
            .getLogger(TPCommonSiteCtrImpl.class);

    private SiteCtrlService siteCtrlService = null;

    @Override
    public ConnectSitesResponse connectSites(ConnectSites parameters)
    {
        ConnectSitesResponse response = new ConnectSitesResponse();
        SDKErrorCode sdkResult = null;
        // 转换:将协议相关的Bean转换为Domain Bean
        try
        {
            if(null == parameters || null == parameters.getSiteUris())
            {
                response.setResultCode(ErrInfo.SDK_PARAM_NOT_COMPLETE_ERRORCODE);
                return response;
            }
            sdkResult = siteCtrlService.connectSites(parameters.getConfId(),
                    parameters.getSiteUris());
        }
        catch (SDKException ex)
        {
            sdkResult = new SDKErrorCode();
            LOGGER.error("connectSites method error", ex);
            sdkResult.setErrCode((int) ex.getSdkErrCode());
        }
        catch (Exception e)
        {
            sdkResult = new SDKErrorCode();
            LOGGER.error("connectSites method error", e);
            sdkResult.setErrCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
        }
        response.setResultCode((int) sdkResult.getErrCode());
        return response;
    }

    @Override
    public MicMuteSitesResponse micMuteSites(MicMuteSites parameters)
    {
        MicMuteSitesResponse response = new MicMuteSitesResponse();
        SDKErrorCode sdkResult = null;
        // 转换:将协议相关的Bean转换为Domain Bean
        try
        {
            if(null == parameters || null == parameters.getSiteUris())
            {
                response.setResultCode(ErrInfo.SDK_PARAM_NOT_COMPLETE_ERRORCODE);
                return response;
            }
            sdkResult = siteCtrlService.muteSites(parameters.getConfId(),
                    parameters.getSiteUris(), parameters.isIsMute());
        }
        catch (SDKException ex)
        {
            sdkResult = new SDKErrorCode();
            LOGGER.error("connectSites method error", ex);
            sdkResult.setErrCode((int) ex.getSdkErrCode());
        }
        catch (Exception e)
        {
            sdkResult = new SDKErrorCode();
            LOGGER.error("micMuteSites method error", e);
            sdkResult.setErrCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
        }
        response.setResultCode((int) sdkResult.getErrCode());
        return response;
    }

    @Override
    public DisconnectSitesResponse disconnectSites(DisconnectSites parameters)
    {
        DisconnectSitesResponse response = new DisconnectSitesResponse();
        SDKErrorCode sdkResult = null;
        // 转换:将协议相关的Bean转换为Domain Bean
        try
        {
            if(null == parameters || null == parameters.getSiteUris())
            {
                response.setResultCode(ErrInfo.SDK_PARAM_NOT_COMPLETE_ERRORCODE);
                return response;
            }
            sdkResult = siteCtrlService.disconnectSites(parameters.getConfId(),
                    parameters.getSiteUris());
        }
        catch (SDKException ex)
        {
            sdkResult = new SDKErrorCode();
            LOGGER.error("connectSites method error", ex);
            sdkResult.setErrCode((int) ex.getSdkErrCode());
        }
        catch (Exception e)
        {
            sdkResult = new SDKErrorCode();
            LOGGER.error("disconnectSites method error", e);
            sdkResult.setErrCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
        }
        response.setResultCode((int) sdkResult.getErrCode());
        return response;
    }

    @Override
    public SpkQuietSitesResponse spkQuietSites(SpkQuietSites parameters)
    {
        SpkQuietSitesResponse response = new SpkQuietSitesResponse();
        SDKErrorCode sdkResult = null;
        // 转换:将协议相关的Bean转换为Domain Bean
        try
        {
            if(null == parameters || null == parameters.getSiteUris())
            {
                response.setResultCode(ErrInfo.SDK_PARAM_NOT_COMPLETE_ERRORCODE);
                return response;
            }
            sdkResult = siteCtrlService.quietSites(parameters.getConfId(),
                    parameters.getSiteUris(), parameters.isIsQuiet());
        }
        catch (SDKException ex)
        {
            sdkResult = new SDKErrorCode();
            LOGGER.error("connectSites method error", ex);
            sdkResult.setErrCode((int) ex.getSdkErrCode());
        }
        catch (Exception e)
        {
            sdkResult = new SDKErrorCode();
            LOGGER.error("spkQuietSites method error", e);
            sdkResult.setErrCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
        }
        response.setResultCode((int) sdkResult.getErrCode());
        return response;
    }

    @Override
    public CtrlAuxStreamResponse ctrlAuxStream(CtrlAuxStream parameters)
    {
        CtrlAuxStreamResponse res = new CtrlAuxStreamResponse();
        SDKErrorCode result = null;
        try
        {
            if(null == parameters)
            {
                res.setResultCode(ErrInfo.SDK_PARAM_NOT_COMPLETE_ERRORCODE);
                return res;
            }
            result = siteCtrlService.ctrlAuxStream(parameters.getSiteUri(),
                    parameters.getControlCode());
        }
        catch (SDKException ex)
        {
            result = new SDKErrorCode();
            LOGGER.error("connectSites method error", ex);
            result.setErrCode((int) ex.getSdkErrCode());
        }
        catch (Exception e)
        {
            result = new SDKErrorCode();
            LOGGER.error("ctrlAuxStream method error", e);
            result.setErrCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
        }

        res.setResultCode((int) result.getErrCode());
        return res;
    }

    @Override
    public CtrlCameraResponse ctrlCamera(CtrlCamera parameters)
    {
        CameraControl control = new CameraControl();
        SDKErrorCode result = null;
        CtrlCameraResponse ctrlCameraResponse = new CtrlCameraResponse();
        try
        {
            if(null == parameters || null == parameters.getCameraControl())
            {
                ctrlCameraResponse.setResultCode(ErrInfo.SDK_PARAM_NOT_COMPLETE_ERRORCODE);
                return ctrlCameraResponse;
            }
            // A: 转换:将协议相关的Bean转换为Domain Bean
            if (null == parameters.getCameraControl().getCamAction()
                    || null == parameters.getCameraControl().getCamPos()
                    || null == parameters.getCameraControl().getCamSrc()
                    || null == parameters.getCameraControl().getCamState())
            {
                result = new SDKErrorCode();
                result.setErrCode(ErrInfo.SDK_PARAM_NOT_COMPLETE_ERRORCODE);
            }
            else
            {
                control.setCamAction(parameters.getCameraControl()
                        .getCamAction());
                control.setCamPos(parameters.getCameraControl().getCamPos());
                control.setCamSrc(parameters.getCameraControl().getCamSrc());
                if (0 == parameters.getCameraControl().getCamState())
                {
                    control.setCamState("localCam");
                }
                else
                {
                    control.setCamState("remoteCam");
                }
                result = siteCtrlService.ctrlCamera(parameters.getSiteUri(),
                        control);
            }
        }
        catch (SDKException ex)
        {
            result = new SDKErrorCode();
            LOGGER.error("connectSites method error", ex);
            result.setErrCode((int) ex.getSdkErrCode());
        }
        catch (Exception e)
        {
            result = new SDKErrorCode();
            LOGGER.error("ctrlCamera method error", e);
            result.setErrCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
        }
        ctrlCameraResponse.setResultCode((int) result.getErrCode());

        return ctrlCameraResponse;
    }

    public SiteCtrlService getSiteCtrlService()
    {
        return siteCtrlService;
    }

    public void setSiteCtrlService(SiteCtrlService siteCtrlService)
    {
        this.siteCtrlService = siteCtrlService;
    }
}
